package br.com.esporte.view.infra.component.spinner;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.PackageResourceReference;

import br.com.esporte.view.infra.component.ComponenteGenerico;

/**
 * 
 * @author Milton Domingues
 *
 */
public class InputSpinner extends ComponenteGenerico {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String SPINNER_UI_CSS = "css/ui.spinner.css";
	private static final String SPINNER_UI_JS = "js/ui.spinner.js";
	private static final String SPINNER_UI_MIN_JS = "js/ui.spinner.min.js";

	private static final String SPINNER_JS_ID = "spinnerJs";
	public static final String VALUE = "value";
	private static final String MIN = "min";
	private static final String MAX = "max";
	private static final String STEP = "step";


	private Integer value;
	private int maxValue;
	private int minValue;
	private int step;


	public InputSpinner(String id, int value, int maxValue, int minValue, int step) {
		super(id);
		this.value = value;
		this.minValue = minValue;
		this.maxValue = maxValue;
		this.step = step;
		setOutputMarkupId(true);
		montarComponente();
	}

	public int getMaxValue() {
		return maxValue;
	}

	public int getMinValue() {
		return minValue;
	}

	public int getStep() {
		return step;
	}

	public Integer getValue() {
		Integer value = Integer.parseInt((String) get(VALUE).getDefaultModelObject());
		return value;
	}

	private void modifierSpinnerParam(TextField<Integer> spinner){
		spinner.add(new SimpleAttributeModifier(MAX, String.valueOf(maxValue)));
		spinner.add(new SimpleAttributeModifier(MIN, String.valueOf(minValue)));
		spinner.add(new SimpleAttributeModifier(STEP, String.valueOf(step)));
		spinner.add(new SimpleAttributeModifier(VALUE, String.valueOf(value)));
	}

	@Override
	public void montarComponente() {
		TextField<Integer> spinner = new TextField<Integer>(VALUE, new Model<Integer>(0));
		spinner.setOutputMarkupId(true);
		modifierSpinnerParam(spinner);
		add(spinner);

		Label js = new Label(SPINNER_JS_ID, spinnerJs(spinner.getMarkupId()));
		js.setEscapeModelStrings(false);
		add(js);
	}

	@Override
	public void renderHead(IHeaderResponse response) {
		response.renderCSSReference(new PackageResourceReference(InputSpinner.class, SPINNER_UI_CSS));
		response.renderJavascriptReference(new PackageResourceReference(InputSpinner.class, SPINNER_UI_JS));
		response.renderJavascriptReference(new PackageResourceReference(InputSpinner.class, SPINNER_UI_MIN_JS));
	}

	@SuppressWarnings("unchecked")
	public void setValue(Integer value){
		TextField<Integer> spinner = (TextField<Integer>) get(VALUE);
		spinner.setDefaultModelObject(value);
	}

	private String spinnerJs(String id){
		StringBuilder js = new StringBuilder();
		js.append("jQuery().ready(function($) {\n");
		js.append("		$('#");
		js.append(id);
		js.append("').spinner();\n");
		js.append("});\n");
		return js.toString();
	}
}

